package com.zto.tbschedule.console.help;

import com.zto.tbschedule.console.vo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class LoginHelp {
    public static final String USER_INFO_KEY="USER_INFO";
    public static final List<String> ignoredUrlList = new ArrayList<String>();
    static {
        ignoredUrlList.add("/user/login.do");
        ignoredUrlList.add(".js");
        ignoredUrlList.add(".css");
        ignoredUrlList.add(".html");
        ignoredUrlList.add(".js.map");
        ignoredUrlList.add(".eot");
        ignoredUrlList.add(".svg");
        ignoredUrlList.add(".ttf");
        ignoredUrlList.add(".woff");
        ignoredUrlList.add(".woff2");
        ignoredUrlList.add(".eot");
        ignoredUrlList.add(".eot");
        ignoredUrlList.add(".eot");
        ignoredUrlList.add(".eot");

    }
    public static boolean ignored(String url){
        for (String ignoredUrl : ignoredUrlList){
            if(url.endsWith(ignoredUrl)){
                return true;
            }
        }
        return false;
    }
    public static User getUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        return (User)session.getAttribute(USER_INFO_KEY);
    }
    public static void setUser(HttpServletRequest request,User user){
        HttpSession session = request.getSession();
        session.setAttribute(USER_INFO_KEY,user);
    }
    public static boolean isAdmin(HttpServletRequest request){
        return isAdmin(getUser(request));
    }
    public static boolean isAdmin(User user){
        if(user==null){
            return false;
        }
        if(user.getRole()==null){
            return false;
        }
        return user.getRole().equals("管理员");
    }
}
